'use strict';
var fs = require('fs');

require('./package.json');

var gruntConfig = {};
try {
    gruntConfig = require('./grunt-config.js');
} catch (e) {
    gruntConfig = require('./grunt-config.default.js');
}


module.exports = function (grunt) {
    // load all grunt tasks
    require('load-grunt-tasks')(grunt);
    require('time-grunt')(grunt);

    // Project configuration.
    grunt.initConfig({
        watch: {
            styles: {
                files: ["static/less/**","catalog/view/theme/**/less/**","admin/view/less/**","index/less/**"],
                tasks: ["less:dev"],
                options: {
                    livereload: false, //default port 35729
                    nospaces: true
                }
            },
            resources: {
                files: ["*",'!less/*'],
                options: {
                    livereload: true
                }
            }
        },
        less: {
            dev: {
                options: {
                    compress: false,
                    yuicompress: false,
                    optimization: 2,
                    sourceMap: true,
                    sourceMapRootpath: gruntConfig.lessMapUrlPrefix,
                    paths: ["static/less/"]
                },
                files: {
                    "static/css/stylesheet.css": "static/less/stylesheet.less"
                }
            },
            prod: {
                options: {
                    compress: true,
                    yuicompress: true,
                    optimization: 2,
                    sourceMap: true,
                    sourceMapRootpath: gruntConfig.lessMapUrlPrefix,
                    paths: ["static/less/","catalog/view/theme/**/less/","admin/view/less/**"]
                },
                files: {
                    "static/css/stylesheet.min.css": "static/less/stylesheet.less"
                }
            }
        },
        postcss: {
            options: {
                map: true, // inline sourcemaps

                // or
                //map: {
                //    inline: false, // save all sourcemaps as separate files...
                //    annotation: 'static/css/maps/' // ...to the specified directory
                //},

                processors: [
                    require('pixrem')(), // add fallbacks for rem units
                    require('autoprefixer')({browsers: 'last 3 versions'}), // add vendor prefixes
                    require('cssnano')() // minify the result
                ]
            },
            dist: {
                src: ['static/css/stylesheet.css','static/css/stylesheet.min.css']
            }
        }
    });

    grunt.registerTask('default', ['less:dev','watch']);
    grunt.registerTask('prebuild', ['postcss:dist']);
    grunt.registerTask('build', ['less:prod']);

};
